package com.liwen.service.file;
import com.liwen.service.util.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author: li.rui
 * @time: 2018-04-18 16:52
 * @description:
 */
public class FileProducer implements IProducer<List<String>> {
    private Logger log = LoggerFactory.getLogger(this.getClass());
    private BufferedReader reader;
    private int line = 100;
    private volatile int skip = 0;
    private volatile boolean hasMore = true;

    public FileProducer(byte[] bytes) throws Exception {
        reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(bytes)));
    }

    public FileProducer(byte[] bytes, int skip, int line) throws Exception {
        reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(bytes)));
        this.skip = skip;
        this.line = line;
    }

    public FileProducer(String filePath) throws Exception {
        reader = new BufferedReader(new FileReader(filePath));
    }

    public FileProducer(String filePath, int skip, int line) throws Exception {
        this(filePath);
        this.skip = skip;
        this.line = line;
    }


    @Override
    public List<String> produce() {
        try {
            if (!hasMore) {
                return null;
            }
            if (skip > 0) {
                while (skip > 0) {
                    reader.readLine();
                    skip--;
                }
            }
            int count = 0;
            List<String> list = null;
            if (line > 200) {
                list = new LinkedList<>();
            } else {
                list = new ArrayList<>(line);
            }
            while (count < line) {
                count++;
                String line = reader.readLine();
                if (null == line) {
                    hasMore = false;
                    break;
                } else {
                    list.add(line);
                }
            }
            if (!hasMore) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (list.isEmpty())
                return null;
            return list;
        } catch (Exception e) {
            log.error("yichang", e);
            return null;
        }
    }

//    public static void main(String[] args) throws Exception {
//        BufferedWriter bw = new BufferedWriter(new FileWriter(new File("D:/data/test.txt")));
//        for (int i = 0; i < 1000; i++) {
//            bw.write(i + "");
//            if ((i & 1) == 1)
//                bw.newLine();
//        }
//        bw.flush();
//        bw.close();
//        FileProducer fileProducer = new FileProducer(FileUtil.FileToByte(new File("D:/data/test.txt")), 0, 10);
//        Storage<List<String>> storage = new Storage(fileProducer);
//        Consumer<List<String>> consumer = new Consumer(storage) {
//            @Override
//            public void consume(Object product) {
//                System.out.println(Thread.currentThread().getName());
//                System.out.println(product.toString());
//            }
//
//            @Override
//            protected long sleep() {
//                return 0;
//            }
//        };
//        Thread thread1 = new Thread(consumer);
//        thread1.start();
//        Thread thread2 = new Thread(consumer);
//        thread2.start();
//        Thread thread3 = new Thread(consumer);
//        thread3.start();
//        ConcurrentHashMap map = new ConcurrentHashMap();
//    }
}
